//
//  ViewController2.swift
//  TarBarJump
//
//  Created by 郑东喜 on 2016/12/12.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit
import WebKit



class ViewController2: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = UIColor.white
        let urlStr = "http://yungou.ie1e.com/app/index.aspx?devtype=1" + "&token="

        let url1 = URL.init(string: urlStr)

        if mainIndexArray.count > 1 {
            //取出数组的url转为字符串
            url = mainIndexArray.lastObject as! String
            
            webView.load(URLRequest.init(url: URL.init(string: url)!))
        } else {
            self.urlRequestCache = NSURLRequest.init(url: url1!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 0)
            
            self.webView.load(self.urlRequestCache as URLRequest)
        }
        
        
    }
    
    

    
    override func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        url = (navigationAction.request.url?.absoluteString)!
        
        print(url)

        if navigationAction.navigationType == WKNavigationType.linkActivated {

            print(mainIndexArray)


            mainIndexArray.add(self.url)
            self.aaa(str: mainIndexArray.lastObject as! String)
            
            decisionHandler(.cancel)
            
        } else {
            decisionHandler(.allow)
        }
    }
    
    //url---
    func aaa(str : String) -> Void {
        
        let vvv = VReplaceVC()
        vvv.url = str
        
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vvv, animated: true)
        self.hidesBottomBarWhenPushed = false
        
    }

}
